package com.qidian.mes.dv.controller;

import com.alibaba.excel.EasyExcel;
import com.qidian.mes.dv.service.MachineryTypeService;
import com.qidian.mes.pojo.MachineryType;
import com.qidian.mes.pojo.Tool;
import com.qidian.mes.util.Tools;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author 陆雯城
 * @Description
 * @Time 2024-04-11 16:21
 */
@CrossOrigin
@RestController
@RequestMapping("/machineryType")
public class MachineryTypeController {
    @Autowired
    private MachineryTypeService machineryTypeService;
    @GetMapping("/export")
    public void export(MachineryType machineryType, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpServletResponse response) throws IOException {
        // 查询数据
        QdResult<List<MachineryType>> qdResult = machineryTypeService.queryAll();
        List<MachineryType> machineryTypes = qdResult.getData();
        // 导出
        try {
            // 设置contentType为excel的格式
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("设备类型信息-" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            // 让文件以附件的形式下载
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), MachineryType.class).sheet("设备类型信息").doWrite(machineryTypes);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(Tools.object2json(QdResult.error("下载文件失败")));
        }
    }

    @GetMapping("/delete")
    public QdResult deleteByMachineryTypeId(Long machineryTypeId) {
        return machineryTypeService.deleteByMachineryTypeId(machineryTypeId);
    }
    @PostMapping("/update")
    public QdResult updateMachineryType(@RequestBody MachineryType machineryType) {
        return machineryTypeService.updateMachineryType(machineryType);
    }

    @RequestMapping("/queryById")
    public QdResult queryById(Long machineryTypeId) {
        return machineryTypeService.queryMachineryTypeById(machineryTypeId);
    }

    @RequestMapping("/save")
    public QdResult saveSubject(@RequestBody MachineryType machineryType) {
        System.out.println(machineryType);
        return machineryTypeService.saveMachineryType(machineryType);
    }

    @GetMapping("/queryMachineryTypeNames")
    public QdResult queryMachineryTypeNames() {
        return machineryTypeService.queryMachineryTypeNames();
    }

    //@GetMapping("/query")
    //public QdResult query(Long parentTypeId,String machineryTypeName,Integer enableStatus) {
    //    return machineryTypeService.queryByParentTypeId(parentTypeId,machineryTypeName,enableStatus);
    //}
    @GetMapping("/query")
    public QdResult query(MachineryType machineryType) {
        return machineryTypeService.queryByParentTypeId(machineryType);
    }
}
